home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n4.arc
/
METAFILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
4KB
|
127 lines
/*--- Listing 2 -------------------------- METAFILE.C -----------
*
* Author: Gary A. Parker
* Last update: 3-12-91
* Compilers: Turbo C++ 1.0, MSC 6.0
* Memory model: any
*
* A demonstration program for the metaphone().
* The program will search a drive for a filename
* which sounds similar to the name passed.
*
* This code is hereby placed in the public domain.
*--------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#if defined(__TURBOC__) /* Turboc C */
#include <dir.h>
typedef struct ffblk FILEINFO;
#define F_FIRST(a,b,c) findfirst(a,c,b)
#define F_NEXT findnext
#define F_ATTRIB ff_attrib
#define F_DATE ff_fdate
#define F_SIZE ff_fsize
#define F_NAME ff_name
#else
typedef struct find_t FILEINFO; /* MSC */
#define F_FIRST _dos_findfirst
#define F_NEXT _dos_findnext
#define F_ATTRIB attrib
#define F_DATE wr_date
#define F_SIZE size
#define F_NAME name
#endif
/* file attributes */
#define RDONLY 0x01 /* Read only file */
#define HIDDEN 0x02 /* Hidden file */
#define SYSTEM 0x04 /* System file */
#define SUBDIR 0x10 /* Subdirectory */
#define ARCH 0x20 /* Archive file */
/* display the file and path if it matches */
void print_find_t(char *dir, FILEINFO *find);
/* perform metaphone comparison */
int metaphone(char *name, char *m, int first);
/* search dos file system recursively */
void search(char *dir, char *name);
int found = 0;
char meta[8]; /* storage for first metaph */
main(int argc, char *argv[])
{
char *ptr;
if(argc == 1) {
puts("syntax metaph [filename]");
exit(1);
}
/* remove extension if any */
if((ptr = memchr(argv[1], '.', 9)) != 0) *ptr = 0;
metaphone(argv[1], meta, 1); /* store the metaph */
printf("\nmetaph for %s is %s\n", argv[1], meta);
search("", ""); /* search for matchs */
if(!found) printf("A match was not found\n");
return 0;
}
void search(char *dir, char *newdir)
{
char curdir[80];
FILEINFO find;
char *ptr;
strcpy(curdir, dir); /* build first pathname */
strcat(curdir, newdir);
strcat(curdir, "\\*.*");
/* check first file */
if( !F_FIRST( curdir, 0xFF, &find) ) {
curdir[strlen(curdir) - 3] = 0; /* remove *.* */
do {
if(find.F_NAME[0] == '.') continue;
else{ /* skip extension */
if((ptr = memchr(find.F_NAME, '.', 9)) != 0)
*ptr = 0;
/* call metaphone with flag set to
0 to compare string with stored
metaph */
if(metaphone(find.F_NAME, meta, 0) == 0) {
/* put back extension */
if(ptr != 0) *ptr = '.';
print_find_t(curdir, &find);
}
}
if((find.F_ATTRIB & SUBDIR))
search(curdir, find.F_NAME); /* recursion */
} while( !F_NEXT( &find ) );
}
}
/* display the file information */
void print_find_t(char *dir, FILEINFO *find)
{
printf("%s%-12s %8ld %2.2d-%02.2d-%02.2d %c%c%c%c%c\r\n",
dir, find->F_NAME, find->F_SIZE,
(find->F_DATE >> 5) & 0x0f,
find->F_DATE & 0x1f,
((find->F_DATE >> 9) + 80) % 100,
(find->F_ATTRIB & SUBDIR) ? 'D' : '.',
(find->F_ATTRIB & RDONLY) ? 'R' : '.',
(find->F_ATTRIB & HIDDEN) ? 'H' : '.',
(find->F_ATTRIB & SYSTEM) ? 'S' : '.',
(find->F_ATTRIB & ARCH) ? 'A' : '.' );
found = 1;
}